package com.app.team.liquid;

//============================================================================
//Name        : TLParse
//Author      : Kyle Bartush
//Version     : 0.1
//Description : Handles the parsing of the HTML returned by the various
//              http requests.
//============================================================================

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.regex.Pattern;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;

public class TLParse {

	private final List<ForumThread> threadsList;
	private final List<ForumPost> postsList;
	private String pageSource;
	boolean news;
	private HttpClient client;

	public TLParse(Context context) {
		threadsList = new ArrayList<ForumThread>();
		postsList = new ArrayList<ForumPost>();
		news = false;
		client = ((LiquidApp)context).getClient();
		pageSource = "";
	}

	public List<ForumThread> getThreadsList() {
		return this.threadsList;
	}

	public List<ForumPost> getPostsList() {
		return this.postsList;
	}

	public String getString() {
		String list = "";
		ListIterator<ForumThread> itr = (ListIterator<ForumThread>) threadsList
				.listIterator();
		while (itr.hasNext()) {
			list += itr.next().toString();
			list += "\n";
		}

		return list;
	}

	public void setHtmlByUrl(String url) throws IOException {
		HttpGet request = new HttpGet(url);
		HttpResponse response = client.execute(request);
		
		InputStream in = response.getEntity().getContent();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		StringBuilder str = new StringBuilder();
		String line = null;
		while ((line = reader.readLine()) != null) {
			str.append(line);
		}
		in.close();
		pageSource = str.toString();
	}

	public void parseThreads() {
		Document doc;
		doc = Jsoup.parse(pageSource);
		Elements threads;
		threads = doc.getElementsByAttributeValueContaining("class",
				"forumindex");
		if (!news) {
			parseNormalThreads(threads);
		} else if (news) {
			parseNewsThreads(threads);
		}

	}

	public void setSource(String string) {
		pageSource = string;
	}

	public String getSource() {
		return pageSource;
	}

	public void setNews(boolean news) {
		this.news = news;
	}

	public boolean getNews() {
		return news;
	}

	private void parseNewsThreads(Elements threads) {
		ListIterator<Element> itr = (ListIterator<Element>) threads
				.listIterator();
		while (itr.hasNext()) {
			String url, title, date, author, png;
			png = itr.next().child(0).attr("src");
			url = itr.next().child(2).attr("href");
			title = itr.previous().child(2).text();
			itr.next();
			date = itr.next().text();
			author = itr.next().text();
			itr.next();
			itr.next();
			ForumThread tmp = new ForumThread(url, title, author, date, png);
			threadsList.add(tmp);
		}

	}

	private void parseNormalThreads(Elements threads) {
		ListIterator<Element> itr = (ListIterator<Element>) threads
				.listIterator();
		while (itr.hasNext()) {
			String url, title, date, author, png;
			png = itr.next().child(0).attr("src");
			url = itr.next().child(0).attr("href");
			title = itr.previous().child(0).text();
			itr.next();
			author = itr.next().text();
			itr.next();
			itr.next();
			date = itr.next().text();
			ForumThread tmp = new ForumThread(url, title, author, date, png);
			threadsList.add(tmp);
		}

	}

	public void parsePosts() {
		Document doc;
		ForumPost post;
		doc = Jsoup.parse(pageSource);
		Elements contents, titles;
		contents = doc.getElementsByAttributeValueContaining("class",
				"forumPost");
		titles = doc.getElementsByAttributeValueContaining("class",
				"forummsginfo");
		
		ListIterator<Element> itrContent = (ListIterator<Element>) contents
				.listIterator();
		ListIterator<Element> itrTitle = (ListIterator<Element>) titles
				.listIterator();
		
		if(contents.size() > titles.size()){
			String title = "News:";
			itrContent.next();
			String content = itrContent.next().html();
			post = new ForumPost(title, content, "");
			postsList.add(post);			
		}


		while (itrContent.hasNext()) {
			String postContent;
			String postTitle;
			postContent = itrContent.next().html();
			postContent = formatContent(postContent);
			postTitle = itrTitle.next().html();
			postTitle = formatTitle(postTitle);
			post = new ForumPost(postTitle, postContent, "");
			postsList.add(post);
		}
	}

	private String formatTitle(String title) {

		title = title.replaceFirst("&nbsp;", "");
		title = title.replace("</span>", "");
		title = title.replace("&nbsp;"," " );
		
		Pattern image = Pattern.compile("<img.*/>"); 
		Pattern span = Pattern.compile("<span.*>");
		title = title.replaceAll(image.toString(), "");
		title = title.replaceAll(span.toString(), "");
		return title;
	}
	
	private String formatContent(String content){
		
		return content;
	}

}
